package com.primb.bench.project.home.presenter;

import android.content.Context;

import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.home.contract.LoanPromiseContract;
import com.primb.bench.project.home.entity.PromiseListEntity;
import com.primb.bench.project.home.entity.RequestClientEntity;
import com.primb.bench.project.home.model.LoanPromiseModelImpl;
import com.primb.netlibs.exception.ApiException;

import java.util.List;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/07/10
 */

public class LoanPromisePresenterImpl implements LoanPromiseContract.Presenter {

    private LoanPromiseModelImpl mModel;
    private LoanPromiseContract.View mView;
    private CompositeSubscription subscription;
    private String custId;
    private RequestClientEntity requestEntity;
    private Context context;

    public LoanPromisePresenterImpl(Context context,LoanPromiseContract.View mView, String custId) {
        this.mView = mView;
        this.context = context;
        this.custId = custId;
        mModel = new LoanPromiseModelImpl();
        subscription = new CompositeSubscription();
        requestEntity = new RequestClientEntity();
    }

    @Override
    public void getDataList(String custId) {
        requestEntity.rePage();
        mView.showLoading();
        Subscription subscribe = mModel.getPromiseInfo(requestEntity).subscribe(new HttpSubscriber<List<PromiseListEntity.DataBean>>(context) {
            @Override
            public void onError(ApiException e) {
                mView.showError(e.getMessage());
                mView.hideLoading();
                mView.clearListData();
            }

            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onNext(List<PromiseListEntity.DataBean> objects) {
                mView.clearListData();
                mView.setDataToList(objects);
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public void loadMoreList() {
        requestEntity.setPageAdd();
        Subscription subscribe = mModel.getPromiseInfo(requestEntity).subscribe(new HttpSubscriber<List<PromiseListEntity.DataBean>>(context) {
            @Override
            public void onError(ApiException e) {
                mView.loadMoreField();
                requestEntity.setPageReduce();
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onNext(List<PromiseListEntity.DataBean> objects) {
                mView.loadMoreList(objects);
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public void start() {
        requestEntity.setCustId(custId);
        getDataList(custId);
    }

    @Override
    public void onRecycle() {
        if (subscription != null) {
            subscription.clear();
        }
    }
}